package ee.ttu.agent.smartHouse.services;

import jade.core.behaviours.CyclicBehaviour;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import ee.ttu.agent.smartHouse.agent.SupervisorAgent;
import ee.ttu.agent.smartHouse.util.Constants;
import ee.ttu.agent.smartHouse.util.CustomACLMessage;
import ee.ttu.agent.smartHouse.util.MessageTemplateBuilder;

public class SupervisorAuthorizationRequestBehaviour extends CyclicBehaviour {

	private static final long serialVersionUID = -7193528552785527280L;
	private SupervisorAgent supervisorAgent;
	
	public SupervisorAuthorizationRequestBehaviour(SupervisorAgent supervisorAgent) {
		this.supervisorAgent = supervisorAgent;
	}

	@Override
	public void action() {
		MessageTemplate tm1 = MessageTemplateBuilder.buildTemplate(ACLMessage.REQUEST, Constants.AUTHORIZATION_REQUEST);
		MessageTemplate tm2 = MessageTemplateBuilder.buildTemplate(CustomACLMessage.FROM_USER, Constants.OI_MESSAGES);
		
		ACLMessage msg = myAgent.receive(MessageTemplate.or(tm1, tm2));
		
		if (msg != null) {
			if(msg.getOntology().equals(Constants.AUTHORIZATION_REQUEST)){
				supervisorAgent.handleAuthorizationRequest(msg);
			}
			
			
			if(msg.getOntology().equals(Constants.OI_MESSAGES)){
				supervisorAgent.setMessage(msg.getContent());
			}
		}else{
			block();
		}
	}
	
}
